<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动主题" prop="activityTheme">
        <el-input v-model="queryParams.activityTheme" placeholder="请输入活动主题" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="容纳人数" prop="galleryful">
        <el-input v-model="queryParams.galleryful" placeholder="请输入容纳人数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker clearable v-model="queryParams.activityTime" type="datetime" value-format="yyyy-MM-dd"
          placeholder="请选择活动时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动地点" prop="activityPlace">
        <el-input v-model="queryParams.activityPlace" placeholder="请输入活动地点" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发布状态" prop="publishStatus">
        <el-select v-model="queryParams.publishStatus" placeholder="请选择发布状态" clearable>
          <el-option v-for="dict in dict.type.staff_activities_publish_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动状态" prop="activityStatus">
        <el-select v-model="queryParams.activityStatus" placeholder="请选择活动状态" clearable>
          <el-option v-for="dict in dict.type.staff_activity_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['staff:OneStopCommunityActivities:add']">新增活动</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['staff:OneStopCommunityActivities:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['staff:OneStopCommunityActivities:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['staff:OneStopCommunityActivities:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="OneStopCommunityActivitiesList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'activityTime', order: 'descending'}">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="活动主题" align="center" prop="activityTheme" />
      <el-table-column label="容纳人数" align="center" prop="galleryful" />
      <el-table-column label="报名人数" align="center" prop="numberApplicants" />
      <el-table-column label="活动时间" align="center" prop="activityTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="activityPlace" />
      <el-table-column label="发布状态" align="center" prop="publishStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.staff_activities_publish_status" :value="scope.row.publishStatus" />
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.staff_activity_status" :value="scope.row.activityStatus" />
        </template>
      </el-table-column>
      <el-table-column label="具体要求" align="center" prop="specificRequirements">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.specificRequirements" placement="top">
            <div class="ellipsis">
              {{ scope.row.specificRequirements }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="活动结束反馈" align="center" prop="feedback">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.feedback || '等待活动结束反馈'" placement="top">
            <div class="ellipsis">
              {{ scope.row.feedback || '等待活动结束反馈' }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="活动结束反馈图片" align="center" prop="feedbackImgs" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.feedbackImgs == null">
            <el-tooltip class="item" effect="dark" :content="scope.row.feedback || '等待活动结束反馈'" placement="top">
              <div class="ellipsis">
                {{ scope.row.feedback || '等待活动结束反馈' }}
              </div>
            </el-tooltip>
          </div>
          <image-preview v-else :src="scope.row.feedbackImgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['staff:OneStopCommunityActivities:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['staff:OneStopCommunityActivities:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleRecord(scope.row)"
            v-if="scope.row.activityStatus !== 2">查看报名记录</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleFeedback(scope.row)"
            v-else>活动结束反馈</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改一站式社区模块-社区活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="feedbackData !== 2">
        <div style="display: flex; align-items: center;">
          <div style="margin-right: 20px;">
            <el-form-item label="活动主题" prop="activityTheme">
              <el-input v-model="form.activityTheme" placeholder="请输入活动主题" />
            </el-form-item>
            <el-form-item label="活动时间" prop="activityTime">
              <el-date-picker clearable v-model="form.activityTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%;" placeholder="请选择活动时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="发布状态" prop="publishStatus">
              <el-radio-group v-model="form.publishStatus" @change="changePublishStatus">
                <el-radio v-for="dict in dict.type.staff_activities_publish_status" :key="dict.value"
                  :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
          <div>
            <el-form-item label="容纳人数" prop="galleryful">
              <!-- <el-input v-model="form.galleryful" placeholder="请输入容纳人数" /> -->
              <el-input-number v-model="form.galleryful" :min="1"  :max="200" label="请输入容纳人数"
                style="width: 100%;"></el-input-number>
            </el-form-item>
            <el-form-item label="活动地点" prop="activityPlace">
              <el-input v-model="form.activityPlace" placeholder="请输入活动地点" />
            </el-form-item>
            <el-form-item label="活动状态" :prop="form.publishStatus == 1 ? '' : 'activityStatus'">
              <el-radio-group v-model="form.activityStatus">
                <el-radio v-for="dict in dict.type.staff_activity_status" :key="dict.value" :disabled="form.publishStatus == 1"
                  :label="parseInt(dict.value)">{{
                    dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
        <el-form-item label="具体要求" prop="specificRequirements">
          <el-input v-model="form.specificRequirements" rows="4" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报名成功提示" prop="signUpPrompt">
          <el-input v-model="form.signUpPrompt" rows="4" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="活动结束反馈" prop="feedback">
          <el-input v-model="form.feedback" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-else>
        <el-form-item label="活动结束反馈说明" prop="feedback">
          <el-input v-model="form.feedback" type="textarea" rows="5" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动结束反馈图片" prop="feedbackImgs">
          <image-upload v-model="form.feedbackImgs"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOneStopCommunityActivities, getOneStopCommunityActivities, delOneStopCommunityActivities, addOneStopCommunityActivities, updateOneStopCommunityActivities } from "@/api/staff/OneStopCommunityActivities";

export default {
  name: "OneStopCommunityActivities",
  dicts: ['staff_activities_publish_status', 'staff_activity_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一站式社区模块-社区活动表格数据
      OneStopCommunityActivitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityTheme: null,
        galleryful: null,
        activityTime: null,
        activityPlace: null,
        publishStatus: null,
        activityStatus: null,
        specificRequirements: null,
        signUpPrompt: null,
        feedback: null,
        feedbackImgs: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activityTheme: [
          { required: true, message: "活动主题不能为空", trigger: "blur" }
        ],
        galleryful: [
          { required: true, message: "容纳人数不能为空", trigger: "blur" }
        ],
        activityTime: [
          { required: true, message: "活动时间不能为空", trigger: "blur" }
        ],
        activityPlace: [
          { required: true, message: "活动地点不能为空", trigger: "blur" }
        ],
        publishStatus: [
          { required: true, message: "发布状态不能为空", trigger: "change" }
        ],
        activityStatus: [
          { required: true, message: "活动状态不能为空", trigger: "change" }
        ],
        specificRequirements: [
          { required: true, message: "具体要求不能为空", trigger: "blur" }
        ],
      },
      // 活动结束反馈
      feedbackData: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询一站式社区模块-社区活动列表 */
    getList() {
      this.loading = true;
      listOneStopCommunityActivities(this.queryParams).then(response => {
        this.OneStopCommunityActivitiesList = response.rows;
        // this.OneStopRegistrationRecordList.numberApplicants = response.rows.filter(item => item.finalRating === 'A').length
        // 计算报名人数
        this.OneStopCommunityActivitiesList.forEach(element => {
          element.numberApplicants = element.oneStopRegistrationRecords.filter(item => (item.auditStatus === 0 || item.auditStatus === 1)).length
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        activityTheme: null,
        galleryful: null,
        activityTime: null,
        activityPlace: null,
        publishStatus: null,
        activityStatus: null,
        specificRequirements: null,
        signUpPrompt: null,
        feedback: null,
        feedbackImgs: null
      };
      this.feedbackData = 0;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加社区活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const activityId = row.activityId || this.ids
      getOneStopCommunityActivities(activityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改社区活动";
      });
    },
    // 活动结束反馈
    handleFeedback(row) {
      this.reset();
      const activityId = row.activityId || this.ids
      getOneStopCommunityActivities(activityId).then(response => {
        this.form = response.data;
        this.feedbackData = response.data.activityStatus;
        this.open = true;
        this.title = "活动结束反馈";
      });
    },
     // 发布活动选项方式改变触发
    changePublishStatus () {
      // 发布活动选择未发布默认把活动状态也改成未发布
      if (this.form.publishStatus == 1) {
        this.form.activityStatus = -1
      } else {
        this.form.activityStatus = 0
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 检查表单中是否有空字符串
          let hasEmptyString = Object.values(this.form).some(value => {
            return (value !== null && value !== undefined && (typeof value === 'string' ? value.trim() === '' : false));
          });

          if (hasEmptyString) {
            this.$modal.msgError("表单中存在空字符串，请填写完整");
            return;
          }
          if (this.form.activityId != null) {
            updateOneStopCommunityActivities(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOneStopCommunityActivities(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids;
      this.$modal.confirm('是否确认删除一站式社区模块-社区活动编号为"' + activityIds + '"的数据项？').then(function () {
        return delOneStopCommunityActivities(activityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('staff/OneStopCommunityActivities/export', {
        ...this.queryParams
      }, `OneStopCommunityActivities_${new Date().getTime()}.xlsx`)
    },
    handleRecord(row) {
      this.$router.push({
        path: '/staff/oneStop/communityActivities/registrationRecord',
        query: {
          activityId: row.activityId || null,
        },
      })
    }
  }
};
</script>

<style scoped>
.ellipsis {
  overflow: hidden;
  /* 确保超出容器的文本被隐藏 */
  white-space: nowrap;
  /* 防止文本换行 */
  text-overflow: ellipsis;
  /* 超出部分显示省略号 */
}
</style>
